/*
* Purpose: axonnet event interface 
* Created: 2012-03-05 Daly 
*/

#ifndef _XNT_EVENT_H_
#define _XNT_EVENT_H_

#ifdef __cplusplus
extern "C" {
#endif

//timer define
#define XNT_TIMER_INFINITY -1


//ev_mask define
#define XNT_EV_NULL  0
#define XNT_EV_READ  1
#define XNT_EV_WRITE 2
#define XNT_EV_TIMER 4
#define XNT_EV_ERROR 8

struct xnt_event_t;

typedef int xnt_msec_t;   //milli second 
typedef void (*xnt_event_proc)(xnt_event_t* ev, int fd);
typedef void (*xnt_logger_proc)(const char* msg);

//basic item of event API.
struct xnt_event_t 
{
	int fd;
	int mask;                 //the event mask (READ or WRITE, but don't set both in one event)
	xnt_event_proc handler;   //callback handler
	int index;        //index of changelist(used for offset)
	void* data;
};

//event config
struct xnt_conf_t 
{
	int max_fd;   //max available connection
	int timetick;  //timetick
};

//poll device config (kqueue, epoll, select...)
struct xnt_event_driver_t
{
	const char *name;    //event driver name
	int (*init)(xnt_conf_t* cf);	// initial param
	int (*shutdown)(void);					// driver close
	int (*ev_add)(xnt_event_t* ev, int fd, int ev_mask);  // add event
	int (*ev_del)(xnt_event_t* ev, int fd, int ev_mask);		// del event
	int (*ev_process)(int timeval);			// poll event(blocking wait)
};

//global driver struct
extern xnt_event_driver_t *g_event_drv ;

//logger
extern xnt_logger_proc g_log;

//interface
int xnt_init(xnt_conf_t* cf);
void xnt_add_event(xnt_event_t* ev, int fd, int ev_mask);
void xnt_del_event(xnt_event_t* ev, int fd, int ev_mask);
void xnt_shutdown();
int xnt_process();

#ifdef __cplusplus
}
#endif

#endif
